import 'package:get/get.dart';
import '../utils/http/http_client.dart';
import '../services/token_service.dart';

class AuthProvider {
  final HttpClient _httpClient = HttpClient();
  
  // 登录
  Future<String> login(String username, String password) async {
    try {
      final response = await _httpClient.post(
        '/auth/login',
        data: {
          'username': username,
          'password': password,
        },
      );
      
      final token = response['data']['access_token'];
      // 使用TokenService保存token
      Get.find<TokenService>().saveToken(token);
      return token;
    } catch (e) {
      rethrow;
    }
  }
  
  // 退出登录
  Future<void> logout() async {
    try {
      await _httpClient.delete('/auth/logout');
      // 使用TokenService清除token
      Get.find<TokenService>().clearToken();
    } catch (e) {
      rethrow;
    }
  }
  
  // 刷新token
  Future<void> refreshToken() async {
    try {
      await _httpClient.post('/auth/refresh');
    } catch (e) {
      rethrow;
    }
  }
} 